﻿//
// File:    TravModelbase.cs
// Project: Traveler 0.3.x (A Windows file explorer)
//
// Copyright 2011-2012 Kasey Osborn (Kasewick@gmail.com)
// Ms-PL license - Use and redistribute freely
//

using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;

using FileTreeSpace;
using Progress;


namespace TravViewed
{
    // Event subscriber handlers:

    public delegate void AddressChangedEventHandler ();

    public delegate void FolderOpenedEventHandler (DirRoot root);
    public delegate void FolderClosedEventHandler ();
    public delegate void StatusChangedEventHandler ();

    public delegate void FolderCreatedEventHandler (string newPathName, string newLeafName);
    public delegate void FolderRenamedEventHandler (string oldPathName, string oldLeafName, string newLeafName);
    public delegate void FolderRemovedEventHandler (string folderName);

    public delegate void FileAddedEventHandler (string newBaseName, long newSize, string newHash, string newPathName, DateTime dateModified, FileAttributes newAttrib);
    public delegate void FileChangedEventHandler (string fullName, long newSize, FileAttributes newAttributes);
    public delegate void FileRenamedEventHandler (string oldFullName, string newFullName, string newBaseName);
    public delegate void FileRemovedEventHandler (string fullName);

    public delegate void DetailsInvalidatedEventHandler ();


    /// <summary>
    /// Define the Modelbase sub-component of the Strict MVC pattern for the Traveler project.
    /// This class provides change notifications and a minimal, read-only interface of the model to the view.
    /// </summary>
    public abstract class TravModelbase
    {
        private IAppView view;
        public IAppView View
        {
            get { return view; }
            set
            {
                if (view != null)
                    throw new InvalidOperationException ("view already assigned");

                view = value;
            }
        }

        public abstract DetailModelbase DetailView { get; }

        public string Arg { get; protected set; }
        public DirRoot Tree { get; protected set; }
        public string CurrentDir { get; protected set; }
        public string Status { get; protected set; }
        public string StatusFyi { get; protected set; }

        public string RootDir { get { return Tree.Name; } }
        public bool HasCurrentDir { get { return !String.IsNullOrEmpty (CurrentDir); } }

        public abstract bool IsBusy { get; }

        public DirPath CurrentDirPath
        {
            get
            {
                if (Tree == null || string.IsNullOrEmpty (CurrentDir))
                    return null;

                return new DirPath (Tree, CurrentDir);
            }
        }

        public event FolderOpenedEventHandler FolderOpened;
        public void OnFolderOpened (DirRoot root) { if (FolderOpened != null) FolderOpened (root); }

        public event FolderClosedEventHandler FolderClosed;
        public void OnFolderClosed () { if (FolderClosed != null) FolderClosed (); }

        public event AddressChangedEventHandler AddressChanged;
        public void OnAddressChanged () { if (AddressChanged != null) AddressChanged (); }

        public event FolderCreatedEventHandler FolderCreated;
        public void OnFolderCreated (string newPathName, string newLeafName)
        { if (FolderCreated != null) FolderCreated (newPathName, newLeafName); }

        public event FolderRenamedEventHandler FolderRenamed;
        public void OnFolderRenamed (string oldPathName, string oldLeafName, string newLeafName)
        { if (FolderRenamed != null) FolderRenamed (oldPathName, oldLeafName, newLeafName); }

        public event FolderRemovedEventHandler FolderRemoved;
        public void OnFolderRemoved (string folderName) { if (FolderRemoved != null) FolderRemoved (folderName); }

        public event StatusChangedEventHandler StatusChanged;
        public void OnStatusChanged () { if (StatusChanged != null) StatusChanged (); }
    }
}
